package com.bw.park2301

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.bw.lib_base.mvvm.BaseViewActivity
import com.bw.park2301.databinding.ActivitySplashBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Timer
import kotlin.concurrent.timerTask

class SplashActivity : BaseViewActivity<ActivitySplashBinding>() {
    var timer: Timer ?= null
    var num = 5
    override fun initView() {
        val skip = SPUtils.getInstance().getBoolean("skip", false)
        if (skip){
            start()
        }else{
            timer = Timer()
            lifecycleScope.launch(Dispatchers.IO) {
                timer!!.schedule(timerTask {
                    lifecycleScope.launch(Dispatchers.Main) {
                        binding.time.setText("${num}s")
                        if (num == 0){
                            start()
                        }
                        num--
                    }
                },0,1000)
            }
        }

        binding.skip.setOnClickListener {
            start()
        }
    }

    override fun initData() {

    }
    fun start(){
        SPUtils.getInstance().put("skip",true)
        if (timer!=null){
            timer!!.cancel()
        }
        ARouter.getInstance().build("/app/LoginActivity").navigation()
        finish()
    }
    override fun onDestroy() {
        super.onDestroy()
        if (timer != null){
            timer?.cancel()
            timer = null
        }

    }
}